aboutsummaryrefslogtreecommitdiffstats
path: root/src/pages/blog/[...id].astro
blob: 88c31703d337e79bf6445777a59c3441a0aed3ad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
---
import Breadcrumbs from '@/components/Breadcrumbs.astro'
import Link from '@/components/Link.astro'
import PostHead from '@/components/PostHead.astro'
import PostNavigation from '@/components/PostNavigation.astro'
import SubpostsHeader from '@/components/SubpostsHeader.astro'
import SubpostsSidebar from '@/components/SubpostsSidebar.astro'
import TOCHeader from '@/components/TOCHeader.astro'
import TOCSidebar from '@/components/TOCSidebar.astro'
import { badgeVariants } from '@/components/ui/badge'
import { Button } from '@/components/ui/button'
import Layout from '@/layouts/Layout.astro'
import {
  getAdjacentPosts,
  getAllPostsAndSubposts,
  getCombinedReadingTime,
  getParentId,
  getParentPost,
  getPostReadingTime,
  getSubpostCount,
  getTOCSections,
  hasSubposts,
  isSubpost,
  parseAuthors,
} from '@/lib/data-utils'
import { formatDate } from '@/lib/utils'
import { Icon } from 'astro-icon/components'
import { Image } from 'astro:assets'
import { render } from 'astro:content'

export async function getStaticPaths() {
  const posts = await getAllPostsAndSubposts()
  return posts.map((post) => ({
    params: { id: post.id },
    props: post,
  }))
}

const post = Astro.props
const currentPostId = Astro.params.id
const { Content, headings } = await render(post)
const authors = await parseAuthors(post.data.authors ?? [])

const isCurrentSubpost = isSubpost(currentPostId)
const navigation = await getAdjacentPosts(currentPostId)
const parentPost = isCurrentSubpost ? await getParentPost(currentPostId) : null

const hasChildPosts = await hasSubposts(currentPostId)
const subpostCount = !isCurrentSubpost
  ? await getSubpostCount(currentPostId)
  : 0
const postReadingTime = await getPostReadingTime(currentPostId)
const combinedReadingTime =
  hasChildPosts && !isCurrentSubpost
    ? await getCombinedReadingTime(currentPostId)
    : null

const tocSections = await getTOCSections(currentPostId)
---

<Layout>
  <PostHead slot="head" post={post} />
  {
    (hasChildPosts || isCurrentSubpost) && (
      <SubpostsHeader
        slot="subposts-navigation"
        parentId={isCurrentSubpost ? getParentId(currentPostId) : currentPostId}
      />
    )
  }
  {
    headings?.length > 0 &&
      !(
        isCurrentSubpost &&
        headings.length === 1 &&
        headings[0].text === post.data.title
      ) && <TOCHeader slot="table-of-contents" headings={headings} />
  }

  <section
    class="grid grid-cols-[minmax(0px,1fr)_min(calc(var(--breakpoint-md)-2rem),100%)_minmax(0px,1fr)] gap-y-6"
  >
    <div class="col-start-2">
      <Breadcrumbs
        items={[
          { href: '/blog', label: 'Blog', icon: 'lucide:library-big' },
          ...(isCurrentSubpost && parentPost
            ? [
                {
                  href: `/blog/${parentPost.id}`,
                  label: parentPost.data.title,
                  icon: 'lucide:book-open',
                },
                {
                  href: `/blog/${currentPostId}`,
                  label: post.data.title,
                  icon: 'lucide:file-text',
                },
              ]
            : [
                {
                  href: `/blog/${currentPostId}`,
                  label: post.data.title,
                  icon: 'lucide:book-open-text',
                },
              ]),
        ]}
      />
    </div>

    {
      post.data.image && (
        <Image
          src={post.data.image}
          alt={post.data.title}
          width={1200}
          height={630}
          class="col-span-full mx-auto w-full max-w-5xl object-cover"
        />
      )
    }

    <section class="col-start-2 flex flex-col gap-y-6 text-center">
      <div class="flex flex-col">
        <h1
          class="mb-2 scroll-mt-31 text-3xl leading-tight font-medium sm:text-4xl"
          id="post-title"
        >
          {post.data.title}
        </h1>

        <div
          class="text-muted-foreground divide-border mb-4 flex flex-col items-center justify-center divide-y text-xs sm:flex-row sm:flex-wrap sm:divide-x sm:divide-y-0 sm:text-sm"
        >
          {
            authors.length > 0 && (
              <div class="flex w-full items-center justify-center gap-x-2 py-2 sm:w-fit sm:px-2 sm:py-0 first:sm:pl-0 last:sm:pr-0">
                {authors.map((author) => (
                  <div class="flex items-center gap-x-1.5">
                    <Image
                      src={author.avatar}
                      alt={author.name}
                      width={20}
                      height={20}
                      class="rounded-full"
                    />
                    {author.isRegistered ? (
                      <Link
                        href={`/members/${author.id}`}
                        underline
                        class="text-foreground"
                      >
                        <span>{author.name}</span>
                      </Link>
                    ) : (
                      <span>{author.name}</span>
                    )}
                  </div>
                ))}
              </div>
            )
          }

          <div
            class="flex w-full items-center justify-center gap-2 py-2 sm:w-fit sm:px-2 sm:py-0 first:sm:pl-0 last:sm:pr-0"
          >
            <span>{formatDate(post.data.date)}</span>
          </div>

          <div
            class="flex w-full items-center justify-center gap-2 py-2 sm:w-fit sm:px-2 sm:py-0 first:sm:pl-0 last:sm:pr-0"
          >
            <span>
              {postReadingTime}
              {
                combinedReadingTime &&
                  combinedReadingTime !== postReadingTime && (
                    <span class="text-muted-foreground">
                      {' '}
                      ({combinedReadingTime} total)
                    </span>
                  )
              }
            </span>
          </div>

          {
            subpostCount > 0 && (
              <div class="flex w-full items-center justify-center gap-1 py-2 sm:w-fit sm:px-2 sm:py-0 first:sm:pl-0 last:sm:pr-0">
                <Icon name="lucide:file-text" class="size-3" />
                {subpostCount} subpost{subpostCount === 1 ? '' : 's'}
              </div>
            )
          }
        </div>
        <div class="flex flex-wrap justify-center gap-2">
          {
            post.data.tags &&
              post.data.tags.length > 0 &&
              post.data.tags.map((tag) => (
                <a
                  href={`/tags/${tag}`}
                  class={badgeVariants({ variant: 'muted' })}
                >
                  <Icon name="lucide:hash" class="size-3" />
                  {tag}
                </a>
              ))
          }
        </div>
      </div>

      <PostNavigation
        newerPost={navigation.newer}
        olderPost={navigation.older}
        baseURL="/blog/"
        parentPost={isCurrentSubpost ? navigation.parent : undefined}
      />
    </section>

    {
      tocSections.length > 0 && (
        <TOCSidebar sections={tocSections} currentPostId={currentPostId} />
      )
    }

    <article class="prose col-start-2 max-w-none">
      <Content />
    </article>

    {
      (hasChildPosts || isCurrentSubpost) && (
        <SubpostsSidebar
          parentId={
            isCurrentSubpost ? getParentId(currentPostId) : currentPostId
          }
          className="w-64"
        />
      )
    }

    <PostNavigation
      newerPost={navigation.newer}
      olderPost={navigation.older}
      path="blog"
      parentPost={isCurrentSubpost ? navigation.parent : undefined}
    />
  </section>

  <Button
    variant="outline"
    size="icon"
    className="group fixed right-8 bottom-8 z-50 hidden"
    id="scroll-to-top"
    title="Scroll to top"
    aria-label="Scroll to top"
  >
    <Icon
      name="lucide:arrow-up"
      class="mx-auto size-4 transition-all group-hover:-translate-y-0.5"
    />
  </Button>

  <script>
    document.addEventListener('astro:page-load', () => {
      const scrollToTopButton = document.getElementById('scroll-to-top')
      const footer = document.querySelector('footer')

      if (scrollToTopButton && footer) {
        scrollToTopButton.addEventListener('click', () => {
          window.scrollTo({ top: 0, behavior: 'smooth' })
        })

        window.addEventListener('scroll', () => {
          const footerRect = footer.getBoundingClientRect()
          const isFooterVisible = footerRect.top <= window.innerHeight

          scrollToTopButton.classList.toggle(
            'hidden',
            window.scrollY <= 300 || isFooterVisible,
          )
        })
      }
    })
  </script>
</Layout>

<script>
  const ensureKatexStyles = () => {
    if (!document.querySelector('.katex')) return
    if (document.querySelector('link[href*="katex.min.css"]')) return

    const link = document.createElement('link')
    link.rel = 'stylesheet'
    link.href = 'https://cdn.jsdelivr.net/npm/katex@0.16.22/dist/katex.min.css'
    document.head.appendChild(link)
  }

  document.addEventListener('astro:page-load', ensureKatexStyles)
  document.addEventListener('astro:after-swap', ensureKatexStyles)
</script>